var GetOpenid = require('../../utils/get_openid.js')
const app = getApp()

Page({
  data: {
    schoolname: '点击选择',
    userid: '',
    password: '',
    cop_id: ''

  },
  // 跳转到此页面时会触发次方法 页面首次加载时会触发本方法
  onLoad: function(options) {
    var openId = wx.getStorageSync('openId')
    console.log(options)
    if (options.cop_id) {
      var copid = options.cop_id
    } else {
      var copid = ''
    }
    if (options.schoolname) {
      var school_name = options.schoolname
    } else {
      var school_name = '点击选择'
    }
    this.setData({
      cop_id: copid,
      schoolname: school_name
    })

    if (openId === '') {
      GetOpenid.getopenid()
    }

  },
  onReady: function() {

  },
  onShow: function() {


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  select: function(e) {
    wx.navigateTo({
      url: '../../pages/selectpage/selectpage'
    })
  },

  login: function(e) {
    var openId = wx.getStorageSync('openId')
    wx.showLoading({
      title: '绑定中',
    })
    var _this = this
    wx.request({
      url: app.globalData.serverurl+'/api/user/verifyopen',
      data: {
        'open_id': wx.getStorageSync('openId'),
        'cop_id': this.data.cop_id,
        'passwd': e.detail.value['pass'],
        'uid': e.detail.value['uid'],
        // 'formId': e.detail.formId
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function(res) {
       
        console.log(res)
        if (res.data.data === 0) {
          GetOpenid.get_userinfo_to_main()
          wx.showToast({
            title: '绑定成功',
            icon: 'loading',
            duration: 10000
          })
          setTimeout(function() {
            wx.hideToast()
          }, 2000)
          _this.activateswitch()
          wx.hideLoading()
          wx.switchTab({
            url: '../../pages/index/index'
          })
        } else {
          wx.showModal({
            title: '账号或密码错误',

          })
        }
      },
      fail: function(res) {

      },
      complete: function() {
        wx.hideLoading()
      }

    })


  },
  backtomain: function() {
    wx.navigateBack({ //返回上一页面或多级页面
      delta: 1
    })
  },

  activateswitch:function(){

    var data = {
      'open_id': wx.getStorageSync('openId'),
      'cop_id': this.data.cop_id,
    }
    app.post(app.globalData.serverurl + '/api/user/activate', data).then((res) => {
      console.log(res)
      if (res.status == 'successful') {
        wx.setStorageSync('userId', res.userid)
        wx.setStorageSync('cop_name', res.cop_name)
        wx.setStorageSync('name', res.name)
        wx.setStorageSync('cop_id', res.cop_id)
        wx.switchTab({
          url: '../../pages/index/index',
        })
      }
      else {
        wx.showModal({
          title: '提示',
          content: '切换失败',
        })
      }
    })
  },

})